﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace WellDone
{
    public sealed partial class SettingsDialog : Page
    {
        private Popup dialogPopup;
        private List<SettingsData> settingsDataList = new List<SettingsData>();
        public event EventHandler CloseRequested;
        public SettingsDialog()
        {
            this.InitializeComponent();
        }
        public void init()
        {
            var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            SettingsData sd = new SettingsData();
            // Load current local settings
            sd.Setting = Globals.k_str_settings_show_close_button;
            Object value = localSettings.Values[Globals.k_str_settings_show_close_button];
            if (value != null)
                sd.Value = (string)value;
            // add value to the list
            settingsDataList.Add(sd);
            // Load next setting
            sd = new SettingsData();
            sd.Setting = Globals.k_str_settings_show_print_button;
            value = localSettings.Values[Globals.k_str_settings_show_print_button];
            if (value != null)
                sd.Value = (string)value;
            // add value to the list
            settingsDataList.Add(sd);
            // Load next setting
            sd = new SettingsData();
            sd.Setting = Globals.k_str_settings_use_timeClock_button;
            value = localSettings.Values[Globals.k_str_settings_use_timeClock_button];
            if (value != null)
                sd.Value = (string)value;
            // add value to the list
            settingsDataList.Add(sd);
            // Load next setting
            sd = new SettingsData();
            sd.Setting = Globals.k_str_settings_receipt_print_route;
            value = localSettings.Values[Globals.k_str_settings_receipt_print_route];
            if (value != null)
                sd.Value = (string)value;
            // add value to the list
            settingsDataList.Add(sd);
            // Load next setting
            sd = new SettingsData();
            sd.Setting = Globals.k_str_settings_order_print_route;
            value = localSettings.Values[Globals.k_str_settings_order_print_route];
            if (value != null)
                sd.Value = (string)value;
            // add value to the list
            settingsDataList.Add(sd);
            // Load next setting
            sd = new SettingsData();
            sd.Setting = Globals.k_str_settings_hide_login_links;
            value = localSettings.Values[Globals.k_str_settings_hide_login_links];
            if (value != null)
                sd.Value = (string)value;
            // add value to the list
            settingsDataList.Add(sd);
            // Display data
            lvSettings.ItemsSource = settingsDataList;            
        }
        private void btnDone_Click(object sender, RoutedEventArgs e)
        {
            // Close the dialog
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, EventArgs.Empty);
            }
        }

        private void lvSettings_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Enable the "Edit" button if an item is selected
            btnEdit.IsEnabled = (lvSettings.SelectedItems.Count == 1);
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (lvSettings.SelectedIndex >= 0)
            {
                // Edit selected item...
                EditSettingDialog esd = new EditSettingDialog();
                esd.CloseRequested += EditSettingsDialog_CloseRequested;
                esd.init(settingsDataList[lvSettings.SelectedIndex].Setting, settingsDataList[lvSettings.SelectedIndex].Value);
                // Create a new dialog popup
                esd.Width = Windows.UI.Xaml.Window.Current.Bounds.Width;
                esd.Height = Windows.UI.Xaml.Window.Current.Bounds.Height;
                dialogPopup = new Popup();
                dialogPopup.Child = esd;
                dialogPopup.IsOpen = true;
            }
        }
        private void EditSettingsDialog_CloseRequested(object sender, EventArgs e)
        {
            // Close the dialog...
            dialogPopup.IsOpen = false;
            // Update settings, if not canceled
            EditSettingDialog esd = (EditSettingDialog)sender;
            if(!esd.bCanceled)
            {
                // Save the setting
                var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
                localSettings.Values[settingsDataList[lvSettings.SelectedIndex].Setting] = esd.strValue;
                settingsDataList[lvSettings.SelectedIndex].Value = esd.strValue;
                lvSettings.ItemsSource = null;
                lvSettings.ItemsSource = settingsDataList;
            }
        }
    }
    public class SettingsData
    {
        public string Setting { get; set; }
        public string Value { get; set; }
    }
}
